package controller;

import common.tree.Node;
import common.tree.Tree;
import context.LoginUserCache;
import context.UserContext;
import entity.Function;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import service.impl.FunctionServiceImpl;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * Created by asus on 2017/6/17.
 */
@Controller
@RequestMapping("/function")
public class FunctionController {
    @Autowired
    private FunctionServiceImpl functionService;

    @RequestMapping("/buildMenuTreeForEdit")
    @ResponseBody
    public List<Node> buildMenuTreeForEdit() {
        List<Function> functions=functionService.getAllFunctions();
        //System.out.println(functions);
        Tree tree=new Tree(functions);
        return tree.build();
    }

    @RequestMapping("/buildMenuAccordion")
    @ResponseBody
    public List<Node> buildMenuAccordion(HttpServletRequest request) {
        String username= (String) request.getSession().getAttribute("username");
        System.out.println("buildMenuAccordion--->username="+username);
        return  LoginUserCache.getAccorions(username);
    }

    @RequestMapping("index")
    public String index(){
        System.out.println(UserContext.getCurrent().getUser());
        return "backmanage";
    }
    public static void main(String[] args){
        ApplicationContext applicationContext=new ClassPathXmlApplicationContext("applicationContext.xml");
        FunctionController functionController= (FunctionController) applicationContext.getBean("functionController");
        System.out.println(functionController.buildMenuTreeForEdit());
    }
}
